package com.example.adms.user.controller;

import com.example.adms.user.dto.ChangeParams;
import com.example.adms.user.dto.LoginParams;
import com.example.adms.user.dto.PageParams;
import com.example.adms.user.entity.User;
import com.example.adms.user.service.UserService;
import com.example.adms.common.mail.MailService;
import com.example.adms.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private MailService mailService;

    /**
     * 登录
     * @param loginParams
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody LoginParams loginParams){
        return userService.login(loginParams);
    }

    /**
     * 登出
     * @param token
     * @return
     */
    @GetMapping("/logout")
    public Result logout(@RequestHeader("Authorization") String token){
        return userService.logout(token);
    }
    /**
     * 通过id查用户
     * @param id
     * @return
     */
    @GetMapping("/getUserById")
    public Result getUserById(Integer id){
        return userService.getUserById(id);
    }

    /**
     * 获取验证码
     * @return
     */
    @GetMapping("/vcode")
    public Result getVcode(){
        return userService.getVcode();
    }

    /**
     * 添加、修改用户
     * @param user
     * @return
     */
    @PostMapping("/addOrUpdate")
    public Result addOrUpdateUser(@RequestBody User user){
        return userService.addOrUpdateUser(user);
    }

    /**
     * 头像上传
     * @param file
     * @return
     */
    @PostMapping("/imageUpload")
    public Result imageUpload(MultipartFile file){
        return userService.imageUpload(file);
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @GetMapping("/delUser")
    public Result delUser(Integer id){
        return userService.delUserById(id);
    }

    /**
     * 根据用户名查用户
     * @param userId
     * @return
     */
    @GetMapping("/getByUserId")
    public Result getByUserId(String userId){
        return userService.getByUserId(userId);
    }

    /**
     * 按条件分页查用户列表
     * @param pageParams
     * @return
     */
    @PostMapping("/getAllUser")
    public Result getAllUser(@RequestBody PageParams pageParams){
        return userService.getUserList(pageParams);
    }

    /**
     * 发送邮件
     * @return
     */
    @GetMapping("/mail")
    public Result sendMail(String mail){
        return userService.sendMail(mail);
    }

    /**
     * 修改密码
     * @param changeParams
     * @return
     */
    @PostMapping("/changePwd")
    public Result changePwd(@RequestBody ChangeParams changeParams){
        return userService.changePwd(changeParams);
    }

    /**
     * 从excel读取要添加的用户
     * @param file
     * @return
     */
    @PostMapping("/addUserFromExcel")
    public Result readUserFromExcel(MultipartFile file){
        return userService.readUserFromExcel(file);
    }

    /**
     * 获取部门领导信息
     * @return
     */
    @GetMapping("/getDepartLeader")
    public Result getDepartLeader(){
        return userService.getDepartLeader();
    }
}
